package com.wwg.util.date;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 类描述:
 *
 * @Author:WWG
 * @date:2018年7月27日
 * @Version:1.0
 */
public class DateCalcUtil {
	
	private static final String PARAM_START = "start";

	private static final Logger LOGGER = LoggerFactory.getLogger(DateCalcUtil.class);
	

	public static final String EMPTY = "";
	public static final String SPACE = " ";
	public static final String TIME_STR_170000 = "17:00:00";
	public static final String TIME_STR_235959 = "23:59:59";
	public static final String TIME_STR_000000 = "00:00:00";
	public static final String DATE_PATTERN = "yyyy-MM-dd";
	public static final String DATE_NUMBER_PATTERN = "yyyyMMdd";
	public static final String DATE_TIME = "HHmmss";
	public static final String HH_MM_SS = "HH:mm:ss";
	public static final String TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static final String TIME_PATTERN2 = "yyyyMMddHHmmss";
	public static final String TIME_PATTERN3 = "yyyy-MM-dd HHmmss";
	public static final String TIME_PATTERN4 = HH_MM_SS;
	public static final String DATE_PATTERN1 = "yyyyMMddHHmm";
	public static final String DATE_PATTERN2 = "yyyyMMdd";

	private static int count = 0;

	public static void main(String[] args) throws ParseException {
		//test1
//		DateFormat dateInstance = DateFormat.getDateInstance();
//		try {
//			String source = "2019-1-1";
//			Date stdate = dateInstance.parse(source);
//			Date bgdate = new Date();
//			String bgString = dateInstance.format(bgdate);
//			long daysBetween = daysBetween(stdate, bgdate);
//			LOGGER.info("{}和{}日期间隔为：{}", bgString, source, daysBetween);
//		} catch (ParseException e) {
//			LOGGER.error(e.getMessage());
//		}
		//test2
//		Assert.isTrue("2018-07-26".equals(calcWorkingDay("2018-08-01", 5, null)),"发生异常");
	    
//	    Assert.isTrue(greaterThanOrEqualTo("2019-12-31", "2019-12-30"), "并不是");
	}

	/**
     * @return
     * {"code":10000,"data":0}为工作日 
     * {"code":10000,"data":1}为休息日
     * {"code":10000,"data":2}为节假日
     * @功能描述:
     * @Author:WWG
     * @date:2018年7月27日 下午6:13:38
     * @Version:1.0 
     */
    public static String isHoliday(String date) {
        String api = "http://api.goseek.cn/Tools/holiday?date=";
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> forEntity = restTemplate.getForEntity(api + date, String.class);
        String body = forEntity.getBody();
        JSONObject object = JSONObject.parseObject(body);
        return object.get("data")+"";
    }
	
	/**
	 * @return
	 * @功能描述:两个日期时间范围内的最近的一个工作日
	 * @Author:WWG
	 * @date:2018年7月27日 下午6:28:58
	 * @Version:1.0
	 */
	public static String getLatestWorkDay(String startDay, String endDate) {
		// 默认其实时间为前天开始
		List<String> list = getDate(startDay, endDate);
		for (int i = list.size() - 1; i >= 0; i--) {
			String data = isHoliday(list.get(i));
			if ("0".equals(data)) {
				return list.get(i);
			}
		}
		return null;
	}

	/**
	 * @param startTime
	 * @param endTime
	 * @return 返回两个日期之间的工作日列表
	 * @throws ParseException 
	 * @功能描述:
	 * @Author:WWG
	 * @date:2018年7月31日 下午3:34:04
	 * @Version:1.0
	 */
	public static List<Date> calcWorkingDayList(String startTime, String endTime, String pattern)  {
		ArrayList<Date> workDayList = new ArrayList<>();
		try {
			SimpleDateFormat sdf;
			if (pattern == null) {
				sdf = new SimpleDateFormat(DATE_PATTERN);
			} else {
				sdf = new SimpleDateFormat(pattern);
			}
			Date begDate = sdf.parse(startTime);
			Date endDate = sdf.parse(endTime);
			// 获取起始日至最终日之间的日期列表
			Calendar begCalendar = Calendar.getInstance();
			Calendar endCalendar = Calendar.getInstance();
			begCalendar.setTime(begDate);
			endCalendar.setTime(endDate);
			while (endCalendar.getTimeInMillis() >= begCalendar.getTimeInMillis()) {
				int beg = begCalendar.get(Calendar.DAY_OF_WEEK);
				// 周日为1，周六为7
				if (beg != 7 && beg != 1) {
					Date date = begCalendar.getTime();
					workDayList.add(date);
				}
				begCalendar.add(Calendar.DATE, 1);
			}
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}
		return workDayList;
	}

	/**
	 * @param startTime
	 * @param endTime
	 * @return 返回两个日期之间的工作日数量
	 * @throws ParseException 
	 * @功能描述:
	 * @Author:WWG
	 * @date:2018年7月31日 下午3:34:04
	 * @Version:1.0
	 */
	public static int calcWorkingDayNum(String startTime, String endTime, String pattern)   {
		int count = 0;
		try {
			SimpleDateFormat sdf;
			if (pattern == null) {
				sdf = new SimpleDateFormat(DATE_PATTERN);
			} else {
				sdf = new SimpleDateFormat(pattern);
			}
			Date begDate = sdf.parse(startTime);
			Date endDate = sdf.parse(endTime);
			// 获取起始日至最终日之间的日期列表
			Calendar begCalendar = Calendar.getInstance();
			Calendar endCalendar = Calendar.getInstance();
			begCalendar.setTime(begDate);
			endCalendar.setTime(endDate);
			while (endCalendar.getTimeInMillis() >= begCalendar.getTimeInMillis()) {
				int beg = begCalendar.get(Calendar.DAY_OF_WEEK);
				// 周日为1，周六为7
				if (beg != 7 && beg != 1) {
					count++;
				}
				begCalendar.add(Calendar.DATE, 1);
			}
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}
		return count;
	}

	/**
	 * @param fromDate
	 *            从某个日期开始
	 * @param days
	 *            往前推十个工作日
	 * @param pattern
	 * @return
	 * @throws ParseException
	 * @功能描述:返回推算的日期是哪一天
	 * @Author:WWG
	 * @date:2018年8月1日 下午12:49:37
	 * @Version:1.0
	 */
	public static String calcWorkingDay(String fromDate, int days, String pattern) {
		Calendar calendar = Calendar.getInstance();
		try {
			SimpleDateFormat sdf;
			if (pattern == null) {
				sdf = new SimpleDateFormat(DATE_PATTERN);
			} else {
				sdf = new SimpleDateFormat(pattern);
			}
			Date begDate = sdf.parse(fromDate);
			calendar.setTime(begDate);
			int count = 0;
			while (true) {
				int beg = calendar.get(Calendar.DAY_OF_WEEK);
				// 周日为1，周六为7
				if (beg != 7 && beg != 1 && ++count == days) {
					break;
				}
				calendar.add(Calendar.DATE, -1);
			}
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}
		return dateToString(calendar.getTime());
	}

	/**
	 * @author huhy 2017-7-28
	 * @param start
	 *            起始日期
	 * @param end
	 *            结束日期
	 * @return
	 */
	public static List<String> getDate(String start, String end) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN2);
		List<String> list = new ArrayList<>(); // 保存日期集合
		try {
			Date dateStart = sdf.parse(start);
			Date dateEnd = sdf.parse(end);
			Date date = dateStart;
			Calendar cd = Calendar.getInstance();// 用Calendar 进行日期比较判断
			while (date.getTime() <= dateEnd.getTime()) {
				list.add(sdf.format(date));
				cd.setTime(date);
				cd.add(Calendar.DATE, 1);// 增加一天 放入集合
				date = cd.getTime();
			}
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}
		return list;
	}

	public static String getTodayStr() {
		Calendar cal = Calendar.getInstance();
		Date date = cal.getTime();
		return dateToString(date);
	}

	public static String getYesterdayStr() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, -1);
		Date date = cal.getTime();
		return dateToString(date);
	}

	public static String getTheDayBefYesterdayStr() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, -2);
		Date date = cal.getTime();
		return dateToString8(date);
	}

	/**
	 * 由日期返回yyyy-MM-dd格式的字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTERN);
		return simpleDateFormat.format(date);

	}

	public static String dateToString(Date date, String format) {
		if (date == null)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		return simpleDateFormat.format(date);

	}

	public static String dateToString8(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_NUMBER_PATTERN);
		return simpleDateFormat.format(date);
	}

	/**
	 * 由日期返回HHmmss格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String dateToTime(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME);
		return simpleDateFormat.format(date);
	}

	/**
	 * 由日期返回yyyyMMdd格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String dateToNumber(Date date) {
		return dateToString8(date);
	}

	/**
	 * 由日期返?? dd 格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String getDay(Date date) {
		if (date == null)
			return null;
		return dateToNumber(date).substring(6, 8);
	}

	/**
	 * 由日期返?? mm 格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String getMonth(Date date) {
		if (date == null)
			return null;
		return dateToNumber(date).substring(4, 6);
	}

	/**
	 * 由日期返?? yyyy 格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String getYear(Date date) {
		if (date == null)
			return null;
		return dateToNumber(date).substring(0, 4);
	}

	/**
	 * 由日期返回yyyy-MM-dd HH:mm:ss格式的字符串
	 *
	 * @param time
	 * @return
	 */
	public static String timeToString(Date time) {
		if (time == null)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_PATTERN);
		return simpleDateFormat.format(time);
	}

	/**
	 * 由日期返回yyyyMMddHHmmss格式的字符串
	 *
	 * @param time
	 * @return
	 */
	public static String timeToNumber(Date time) {
		if (time == null)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_PATTERN2);
		return simpleDateFormat.format(time);
	}

	/**
	 * 将date去除时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getDate(Date date) {
		if (date == null)
			return null;
		return stringToDate(dateToString(date));
	}

	/**
	 * 由日期返回yyyy-MM-dd-HH.mm.ss.SSSSSS格式的字符串
	 *
	 * @param date
	 *            Date格式日期
	 * @return String yyyy-MM-dd-HH.mm.ss.SSSSSS格式 的字符串
	 */
	public static String getTimeStampFormat(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS");
		return simpleDateFormat.format(date);
	}

	/**
	 * 由yyyy-MM-dd HH:mm:ss格式的字符串返回日期时间
	 *
	 * @param string
	 *            时间
	 * @return
	 */
	public static Date stringToTime(String string) {
		if (string == null)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_PATTERN);

		try {
			return simpleDateFormat.parse(string);
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}

		return null;
	}

	/**
	 * 由yyyy-MM-dd格式的字符串返回日期
	 *
	 * @param string
	 *            date字符??
	 * @return
	 */
	public static Date stringToDate(String string) {
		if (string == null)
			return null;
		if (string.trim().length() == 0)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTERN);

		try {
			return simpleDateFormat.parse(string);
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}

		return null;
	}

	/**
	 * 由yyyy-MM-dd HHmmss格式的字符串返回日期
	 *
	 * @param string
	 * @return
	 */
	public static Date stringToDate2(String string) {
		if (string == null)
			return null;
		if (string.trim().length() == 0)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_PATTERN3);

		try {
			return simpleDateFormat.parse(string);
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}

		return null;
	}

	/**
	 * 由yyyyMMddHHmmss格式的字符串返回日期
	 *
	 */
	public static Date stringToDate3(String string) {
		if (string == null)
			return null;
		if (string.trim().length() == 0)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_PATTERN2);

		try {
			return simpleDateFormat.parse(string);
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}

		return null;
	}

	/**
	 * 由yyyyMMddHHmmss格式的字符串转化为yyyy-MM-dd HH:mm:ss格式的字符串
	 *
	 * @param string
	 * @return
	 */
	public static String stringToDateString(String string) {
		if (string == null)
			return null;
		StringBuilder builder = new StringBuilder("");
		for (int i = 0; i < 14; i++) {
			builder.append(string.substring(i, i + 1));
			if (i == 3 || i == 5) {
				builder.append("-");
			} else if (i == 7) {
				builder.append(" ");
			} else if (i == 9 || i == 11) {
				builder.append(":");
			}
		}
		return builder.toString();
	}

	/**
	 * 由yyyyMMdd格式的字符串返回日期
	 *
	 * @param string
	 * @return
	 */
	public static Date numberToDate(String string) {
		if (string == null)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_NUMBER_PATTERN);
		try {
			return simpleDateFormat.parse(string);
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}

		return null;
	}

	/**
	 * ??"yyyy-mm-dd"格式的日期字符串转换??"yyyymmdd"的格??
	 *
	 * @param date
	 *            日期
	 * @return "yyyymmdd"的日期字符串
	 */
	public static String dateStringToNumber(String date) {
		return date.replaceAll("-", "");
	}

	/**
	 * ??"yyyymmdd"格式的日期字符串转换??"yyyy-mm-dd"的格??
	 *
	 * @param date
	 *            日期
	 * @return "yyyy-mm-dd"的日期字符串
	 */
	public static String numberToDateString(String date) {
		if (date == null || date.trim().length() != 8 || "00000000".equalsIgnoreCase(date))
			return "";
		else
			return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
	}

	/**
	 * ??"yyyymmddhhmmss"格式的日期字符串转换??"yyyy-mm-dd hh:mm:ss"的格??
	 * 
	 * @param time
	 *            日期时间字符??
	 * @return "yyyy-mm-dd hh:mm:ss"的日期字符串
	 */
	public static String numberToTimeString(String time) {
		if (time == null || time.trim().length() != 14)
			return "";
		else
			return time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8) + " " + time.substring(8, 10)
					+ ":" + time.substring(10, 12) + ":" + time.substring(12, 14);
	}

	/**
	 * ??"hhmmss"格式的日期字符串转换??"hh:mm:ss"的格?? Added by cx 08-09-28
	 *
	 * @param time
	 *            时间
	 * @return "hh:mm:ss"的日期字符串
	 */
	public static String numberToTimeString2(String time) {
		if (time == null || time.trim().length() != 6)
			return "";
		else
			return time.substring(0, 2) + ":" + time.substring(2, 4) + ":" + time.substring(4, 6);
	}

	/**
	 * 由HHmmss格式的字符串返回日期 Added by cx 08-09-28
	 *
	 * @param string
	 *            时间字符??
	 * @return
	 */
	public static Date numberToTime(String string) {
		if (string == null)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_PATTERN4);

		try {
			return simpleDateFormat.parse(string);
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}

		return null;
	}

	/**
	 * ??"yyyy-mm-dd"格式的日期字符串转换??"yyyymm"的格??
	 *
	 * @param date
	 *            Added by llb 08-10-21 日期
	 * @return "yyyymmdd"的日期字符串
	 */
	public static String dateStringToNumber6(String date) {
		return date.replaceAll("-", "").substring(0, 6);
	}

	/**
	 * 返回两日期相隔天??
	 *
	 * @param bgdate
	 * @param enddate
	 * @return
	 */
	public static long daysBetween(Date bgdate, Date enddate) {
		long beginTime = bgdate.getTime();
		long endTime = enddate.getTime();
		return (long) ((endTime - beginTime) / (1000d * 60 * 60 * 24) + 0.5);
	}

	/**
	 * 返回两日期相隔天??
	 *
	 * @param bgdate
	 * @param enddate
	 * @return
	 */
	public static long daysBetween(java.sql.Date bgdate, java.sql.Date enddate) {
		long beginTime = bgdate.getTime();
		long endTime = enddate.getTime();
		return (long) ((endTime - beginTime) / (1000d * 60 * 60 * 24) + 0.5);
	}

	public static Date addDate(Date date, int x) {
		if (date == null)
			return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, x);
		date = cal.getTime();
		return date;
	}

	/**
	 * 当得当天
	 */
	public static java.sql.Date getToday() {
		return new java.sql.Date(System.currentTimeMillis());
	}

	/**
	 * 返回前一个月的同????
	 */
	public static java.sql.Date getMonthAgoDay() {
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		Calendar lastDate = (Calendar) c.clone();
		lastDate.add(Calendar.MONTH, -1);
		Date monthAgoDay = lastDate.getTime();
		return new java.sql.Date(monthAgoDay.getTime());
	}

	/**
	 * 返回??3个月(??)的同????
	 */
	public static java.sql.Date getSeasonAgoDay() {
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		Calendar lastDate = (Calendar) c.clone();
		lastDate.add(Calendar.MONTH, -3);
		Date monthAgoDay = lastDate.getTime();
		return new java.sql.Date(monthAgoDay.getTime());
	}

	/**
	 * @Author Lucien.li 返回传入月数前的同一天 任务 513
	 */
	public static Date getMonthsAgoDay(int months) {
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		Calendar lastDate = (Calendar) c.clone();
		lastDate.add(Calendar.MONTH, -months);
		return lastDate.getTime();
	}

	/**
	 * 返回??1年的同一??
	 */
	public static java.sql.Date getYearAgoDay() {
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		Calendar lastDate = (Calendar) c.clone();
		lastDate.add(Calendar.YEAR, -1);
		Date monthAgoDay = lastDate.getTime();
		return new java.sql.Date(monthAgoDay.getTime());
	}

	public static String getCurrentDateString() {
		String result = null;
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
		Date nowDate = new Date();
		result = sdf.format(nowDate);
		return result;
	}

	public static String convertDateToString(Date date) {
		String result = null;
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
		result = sdf.format(date);
		return result;
	}

	public static Date getCurrentDate() {
		return new Date();
	}

	public static String getCurrentTimeString() {
		String result = null;
		SimpleDateFormat sdf = new SimpleDateFormat("HH-mm-ss");
		Date nowDate = new Date();
		result = sdf.format(nowDate);
		return result;
	}

	public static String getCurrentDateTime() {
		String result = null;
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN);
		Date nowDate = new Date();
		result = sdf.format(nowDate);
		return result;
	}

	public static String getTimeStamp() {
		StringBuilder result = new StringBuilder();
		if (count < 100) {
			count++;
		} else {
			count = 1;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN2);
		Date nowDate = new Date();
		result.append(sdf.format(nowDate)).append(count);
		return result.toString();
	}

	public static String dateFormat(String date) {
		String result = null;
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN);
		Date theDate = null;
		try {
			theDate = sdf.parse(date);
			sdf = new SimpleDateFormat(TIME_PATTERN);
			result = sdf.format(theDate);
		} catch (Exception e) {
			LOGGER.error(e.getMessage());
		}
		return result;
	}

	public static String dateFormat(String date, String format) {
		String result = null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date theDate = null;
		try {
			theDate = sdf.parse(date);
			sdf = new SimpleDateFormat(format);
			result = sdf.format(theDate);
		} catch (Exception e) {
			LOGGER.error(e.getMessage());
		}
		return result;
	}

	public static Date convertStrToDate(String str, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = new Date();
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}
		return date;
	}

	/**
	 * 根据设定的格式来格式化日期
	 *
	 * @param date
	 *            日期
	 * @param format
	 *            格式字符串
	 * @return 日期字符串
	 */
	public static String convertDateToStrByFormat(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (date != null) {
			return sdf.format(date);
		} else {
			return EMPTY;
		}
	}

	public static long getCurrentTime() {
		return (new Date()).getTime();
	}

	/*
	 * 增加1/1000??
	 */
	public static Date addMillisecond(Date dt, int millisecond) {
		return addSecond(dt, (long) millisecond);
	}

	public static Date addMillisecond(Date dt, long millisecond) {
		dt.setTime(dt.getTime() + millisecond);
		return dt;
	}

	/*
	 * 增加??
	 */
	public static Date addSecond(Date dt, int second) {
		return addSecond(dt, (long) second);
	}

	public static Date addSecond(Date dt, float second) {
		return addSecond(dt, (long) second);
	}

	public static Date addSecond(Date dt, long second) {
		return addMillisecond(dt, 1000L * second);
	}

	public static Date addSecond(Date dt, double second) {
		Double millisecond = 1000D * second;
		return addMillisecond(dt, millisecond.longValue());
	}

	/*
	 * 增加分钟
	 */
	public static Date addMinute(Date dt, int minute) {
		return addMinute(dt, (long) minute);
	}

	public static Date addMinute(Date dt, float minute) {
		return addMinute(dt, (long) minute);
	}

	public static Date addMinute(Date dt, long minute) {
		return addMillisecond(dt, 60000L * minute);
	}

	public static Date addMinute(Date dt, double minute) {
		Double millisecond = 60000D * minute;
		return addMillisecond(dt, millisecond.longValue());
	}

	/*
	 * 增加小时
	 */
	public static Date addHour(Date dt, int hour) {
		return addHour(dt, (long) hour);
	}

	public static Date addHour(Date dt, float hour) {
		return addHour(dt, (long) hour);
	}

	public static Date addHour(Date dt, long hour) {
		return addMillisecond(dt, 0x36ee80L * hour);
	}

	public static Date addHour(Date dt, double hour) {
		Double millisecond = 3600000D * hour;
		return addMillisecond(dt, millisecond.longValue());
	}

	/*
	 * 增加??
	 */
	public static Date addDay(Date dt, int day) {
		return addDay(dt, (long) day);
	}

	public static Date addDay(Date dt, float day) {
		return addDay(dt, (long) day);
	}

	public static Date addDay(Date dt, long day) {
		return addMillisecond(dt, 0x5265c00L * day);
	}

	public static Date addDay(Date dt, double day) {
		Double millisecond = 86400000D * day;
		return addMillisecond(dt, millisecond.longValue());
	}

	/*
	 * 增加??
	 */
	public static Date addMonth(Date dt, int month) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		cal.set(2, cal.get(2) + month);

		return cal.getTime();
	}

	public static Date addMonth(Date dt, float month) {
		return addMonth(dt, (int) month);
	}

	public static Date addMonth(Date dt, long month) {
		return addMonth(dt, (Long.valueOf(month)).intValue());
	}

	public static Date addMonth(Date dt, double month) {
		double floorMonth = Math.floor(month);
		double decimalMonth = month - floorMonth;
		dt = addMonth(dt, (Double.valueOf(floorMonth)).intValue());
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		cal.set(2, cal.get(2) + 1);
		Date nextdt = cal.getTime();
		long monthMillisecond = nextdt.getTime() - dt.getTime();
		double millisecond = (double) monthMillisecond * decimalMonth;
		return addMillisecond(dt, (long) millisecond);
	}

	/*
	 * 增加??
	 */
	public static Date addYear(Date dt, int year) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		cal.set(1, cal.get(1) + year);
		return cal.getTime();
	}

	public static Date addYear(Date dt, float year) {
		return addYear(dt, (int) year);
	}

	public static Date addYear(Date dt, long year) {
		return addYear(dt, (Long.valueOf(year)).intValue());
	}

	public static Date addYear(Date dt, double year) {
		double floorYear = Math.floor(year);
		double decimalYear = year - floorYear;
		dt = addYear(dt, (Double.valueOf(floorYear)).intValue());
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		cal.set(1, cal.get(1) + 1);
		Date nextdt = cal.getTime();
		long yearMillisecond = nextdt.getTime() - dt.getTime();
		double millisecond = (double) yearMillisecond * decimalYear;
		return addSecond(dt, (long) millisecond);
	}

	/**
	 * 取当前时间月的第??天日??
	 *
	 * @param date
	 * @return
	 */
	public static Date getFirstDateOfMonth(Date date) {
		Calendar calDate = Calendar.getInstance();
		calDate.setTime(date);
		calDate.set(Calendar.DATE, 1);
		return calDate.getTime();
	}

	/**
	 * 取当前时间月的第??天日??
	 *
	 * @param date
	 * @return
	 */
	public static java.sql.Date getFirstDateOfMonth2(java.sql.Date date) {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date);
		c1.set(Calendar.DATE, 1);
		return new java.sql.Date(c1.getTimeInMillis());
	}

	public static Date getFirstDateOfYear(Date date) {
		Calendar calDate = Calendar.getInstance();
		calDate.setTime(date);
		calDate.set(Calendar.DATE, 1);
		calDate.set(Calendar.MONTH, 1);
		return calDate.getTime();
	}

	public static Calendar getCalendar(Date date) {
		Calendar calDate = Calendar.getInstance();
		calDate.setTime(date);
		return calDate;
	}

	public static Date getFirstDateOfWeek(Date date) {
		Date result = null;
		Calendar calDate = getCalendar(date);
		int firstDay = calDate.get(Calendar.DAY_OF_WEEK);
		result = addDay(calDate.getTime(), -firstDay + 1);
		return result;
	}

	public static Date getLastDateOfWeek(Date date) {
		Date result = null;
		Calendar calDate = getCalendar(date);
		int firstDay = calDate.get(Calendar.DAY_OF_WEEK);
		result = addDay(calDate.getTime(), 7 - firstDay);
		return result;
	}

	/**
	 * 取当前时间月的最后一天日??
	 *
	 * @param date
	 * @return
	 */
	public static Date getLastDateOfMonth(Date date) {
		Calendar calDate = Calendar.getInstance();
		calDate.setTime(date);
		calDate.set(Calendar.DATE, calDate.getActualMaximum(Calendar.DATE));

		return calDate.getTime();
	}

	/**
	 * 取当前时间月的最后一天日??
	 *
	 * @param date
	 * @return
	 */
	public static java.sql.Date getLastDateOfMonth2(java.sql.Date date) {
		Calendar calDate = Calendar.getInstance();
		calDate.setTime(date);
		calDate.setTime(date);
		calDate.set(Calendar.DATE, calDate.getActualMaximum(Calendar.DATE));
		return new java.sql.Date(calDate.getTimeInMillis());
	}

	/**
	 * 取得??个月的天??
	 *
	 * @param date
	 *            strDate
	 * @return
	 */
	public static int getDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.getActualMaximum(Calendar.DATE);

	}

	public static int monthsBetween(java.sql.Date from, java.sql.Date end) {
		try {
			Calendar c1 = Calendar.getInstance();
			Calendar c2 = Calendar.getInstance();
			c1.setTime(from);
			c2.setTime(end);
			return (c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) * 12 + (c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH));
		} catch (Exception e) {
			LOGGER.error(e.getMessage());
			return -1;
		}
	}

	public static Timestamp getCurrentTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}

	public static Timestamp getTimestamp(java.sql.Date date) {
		return new Timestamp(date.getTime());
	}

	public static Timestamp getTimestamp(Date date) {
		return new Timestamp(date.getTime());
	}

	/**
	 * 由yyyy-MM-dd格式的字符串返回日期
	 *
	 * @param string
	 *            date字符??
	 * @return
	 */
	public static java.sql.Date stringToSqlDate(String string) {
		if (string == null)
			return null;
		if (string.trim().length() == 0)
			return null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTERN);

		try {
			Date utilDate = simpleDateFormat.parse(string);
			if (utilDate != null) {
				return new java.sql.Date(utilDate.getTime());
			}
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}

		return null;
	}

	public static String getDateTimeString(Timestamp dateTime) {
		if (dateTime == null) {
			return null;
		}
		String result = null;
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN);
		result = sdf.format(dateTime);
		return result;
	}

	// 取两个日期的较大??
	public static Date maxDate(Date d1, Date d2) {
		Date dMax;
		if (d1.compareTo(d2) >= 0) {
			dMax = d1;
		} else {
			dMax = d2;
		}
		return dMax;
	}

	// 取两个日期的较小??
	public static Date minDate(Date d1, Date d2) {
		Date dMin;
		if (d1.compareTo(d2) <= 0) {
			dMin = d1;
		} else {
			dMin = d2;
		}
		return dMin;
	}
	
	
    /**  
     * @param dateStr1
     * @param dateStr2
     * @return
     * @throws ParseException  
     * @功能描述:Greater than or equal to
     * @Author:WWG
     * @date:2020年1月4日  下午11:19:08
     * @Version:2.2
     */
    public static boolean greaterThanOrEqualTo(String dateStr1, String dateStr2) {
        return greaterThanOrEqualTo(dateStr1, dateStr2, DATE_PATTERN);
    }
	/**  
	 * @param format
	 * @return  
	 * @throws ParseException 
	 * @功能描述:Greater than or equal to
	 * @Author:WWG
	 * @date:2020年1月4日  下午11:09:18
	 * @Version:2.2
	 */
    public static boolean greaterThanOrEqualTo(String dateStr1, String dateStr2, String format) {
        SimpleDateFormat formater = new SimpleDateFormat(format);
        boolean b = false;
        try {
            Date date1 = formater.parse(dateStr1);
            Date date2 = formater.parse(dateStr2);
            if (date1.compareTo(date2) >= 0) {
                b = true;
            }
        } catch (ParseException e) {
            LOGGER.error("日期格式化异常！");
        }
        return b;
    }

	// 两个时间点中间存在时间段
	public static boolean existDateSection(Date dMax, Date dMin) {
		boolean flag = true;
		if (dMax.compareTo(dMin) <= 0) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;
	}

	/**
	 * Time转换为String
	 * 
	 * @param time
	 */

	public static String timeToString(Time time) {

		if (null == time) {
			return null;
		}

		return time.toString().replace(":", "");

	}

	/**
	 * Date()+time返回date
	 * 
	 * @param date
	 *            ,time
	 */
	public static Date dateAddTime(Date date, Time time) {

		return new Date(date.getTime() + time.getTime());
	}

	/**
	 * 根据当前日期获取当前日期所在周次的始末日期
	 * 
	 * @param time
	 * @return
	 */
	public static Date[] getDateTFByDate(Date time) {
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(time);
		} catch (Exception e) {
			LOGGER.error(e.getMessage());
		}
		int d = 0;
		if (cal.get(Calendar.DAY_OF_WEEK) > 4) {
			cal.add(Calendar.DAY_OF_WEEK, 7);
		}
		d = 2 - cal.get(Calendar.DAY_OF_WEEK);
		cal.add(Calendar.DAY_OF_WEEK, d);
		cal.add(Calendar.DAY_OF_WEEK, -4);
		// 所在周开始日期
		Date begin = cal.getTime();
		cal.add(Calendar.DAY_OF_WEEK, 6);
		// 所在周结束日期
		Date end = cal.getTime();
		ArrayList<Date> list = new ArrayList<>();
		list.add(begin);
		list.add(end);
		return list.toArray(new Date[2]);
	}

	/**
	 * 以周四到下周三为准 的下周起始日期和结束
	 * 
	 * @param date
	 *            日期
	 * @param type
	 *            start为开始日期,end为结束日期
	 * @return 当期日期的周四或者下周三的日期 字符串
	 */
	public static String getStartOrEndDate(Date date, String type) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		DateFormat df = new SimpleDateFormat(DATE_PATTERN2);
		int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
		if (dayofweek == 1) {
			dayofweek = 8;
		}
		int end;
		int start;
		if (dayofweek < 5) {
			end = 4 - dayofweek + calendar.get(Calendar.DAY_OF_MONTH);
			calendar.set(Calendar.DAY_OF_MONTH, end);
			if (PARAM_START.equals(type)) {
				calendar.add(Calendar.DAY_OF_MONTH, -6);
			}
			String format = df.format(calendar.getTime());
			LOGGER.info("{}日期为：{}", type, format);
			return df.format(calendar.getTime());
		} else {
			start = calendar.get(Calendar.DAY_OF_MONTH) - (dayofweek - 5);
			calendar.set(Calendar.DAY_OF_MONTH, start);
			String format = df.format(calendar.getTime());
			if (!PARAM_START.equals(type)) {
				calendar.add(Calendar.DAY_OF_MONTH, 6);
			}
			LOGGER.info("{}start日期为：{}", type ,format);
			return df.format(calendar.getTime());
		}
	}

	/**
	 * 获取上一周或下一周的周四-yyyy-MM-dd
	 * 
	 * @param x
	 * @return
	 */
	public static String getWeekThursday(int x, Date erdatDate) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN2);
		Calendar date = Calendar.getInstance();
		date.setTime(erdatDate);
		date.setFirstDayOfWeek(Calendar.MONDAY);// 将每周第一天设为星期一，默认是星期天
		date.add(Calendar.WEEK_OF_MONTH, x);// 周数减一，即上周
		date.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);// 日子设为星期四
		return format.format(date.getTime());
	}

	/**
	 * 通过黄金周周次获取周次对应的两个周四日期 add 2015年8月20日14:47:54
	 * 
	 * @param gdWeek
	 *            ,type
	 * @return
	 */
	public static String getGoldenWeekStartOrEndDate(String gdWeek, String type) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN2);
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);// 将每周第一天设为星期一，默认是星期天
		int weekOfyear = 0;
		int years = 0;
		if (gdWeek != null && !"".equals(gdWeek) && !"000000".equals(gdWeek) && !"null".equals(gdWeek)) {
			String yearStr = gdWeek.substring(0, 4);
			gdWeek = gdWeek.substring(4, 6);
			years = Integer.valueOf(yearStr);
			weekOfyear = Integer.valueOf(gdWeek);
		}
		if (years != 0 && weekOfyear != 0) {
			cal.set(Calendar.YEAR, years);
			cal.set(Calendar.WEEK_OF_YEAR, weekOfyear);
			int dayForWeek = 0;
			if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
				dayForWeek = 7;
			} else {
				dayForWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
			}
			cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
			LOGGER.info("dayForWeek：{}" , dayForWeek);
			int weekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
			LOGGER.info("weekOfYear:{}" , weekOfYear);
			Date time = new Date(cal.getTime().getTime());
			if (PARAM_START.equals(type)) {
				return format.format(cal.getTime());
			} else {
				String nextWeekThursday = getWeekThursday(1, time);
				Date addDate = addDate(numberToDate(nextWeekThursday), -1);
				return format.format(addDate);
			}
		}
		return "";
	}

	/**
	 * 日期转换星期
	 */
	public static String dateWeek(String orderDate) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		String week = "星期";
		SimpleDateFormat sdf = null;
		if (orderDate.indexOf('/') != -1) {
			sdf = new SimpleDateFormat("yyyy/MM/dd");
		} else if (orderDate.indexOf('-') != -1) {
			sdf = new SimpleDateFormat(DATE_PATTERN);
		} else {
			sdf = new SimpleDateFormat(DATE_PATTERN2);
		}
		try {
			date = sdf.parse(orderDate);
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}
		c.setTime(date);
		switch (c.get(Calendar.DAY_OF_WEEK) - 1) {
		case 1:
			week = week + "一";
			break;
		case 2:
			week = week + "二";
			break;
		case 3:
			week = week + "三";
			break;
		case 4:
			week = week + "四";
			break;
		case 5:
			week = week + "五";
			break;
		case 6:
			week = week + "六";
			break;
		case 7:
			week = week + "日";
			break;
		default:
			LOGGER.error("系统异常！");
		}
		return week;
	}

	public static Date formatDate(String dateStr, String formatType) {
		SimpleDateFormat sdf = new SimpleDateFormat(formatType);
		Date date;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
			return null;
		}
		return date;
	}

	/**
	 * 字符串转换成日期
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date formatDate(String dateStr) {
		return formatDate(dateStr, DATE_PATTERN);
	}

	/**
	 * 字符串转换成时间
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date formatTime(String dateStr) {
		return formatDate(dateStr, HH_MM_SS);
	}

	public static String getDate(Date date, String formatType) {
		SimpleDateFormat format = new SimpleDateFormat(formatType);
		return format.format(date);
	}

	public static String getTime(Date date) {
		return getDate(date, HH_MM_SS);
	}

	public static String getTimestampFixedFormat(Date date) {
		return getDate(date, TIME_PATTERN);
	}

	public static String getDateFixedFormat(Date date) {
		return getDate(date, DATE_PATTERN);
	}

	/**
	 * @Author lucien.li
	 * @DATE: 2017/4/5 调用：用于判断字符串是否为日期
	 */
	public static boolean stringIsDate(String str) {
		boolean flag = true;
		if (StringUtils.isEmpty(str)) {
			flag = false;
		} else {
			try {
				SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN2);
				format.setLenient(false);// 严格执行日期校验
				format.parse(str);
				flag = true;
			} catch (ParseException e) {
				flag = false;
			}
		}
		return flag;
	}

	/**
	 * 将yyyyMMdd字符串转为yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static String convertDateStr(String date) {
		DateFormat format1 = new SimpleDateFormat(DATE_NUMBER_PATTERN);
		DateFormat format2 = new SimpleDateFormat(DATE_PATTERN);
		try {
			return format2.format(format1.parse(date));
		} catch (ParseException e) {
			LOGGER.error(e.getMessage());
		}
		return "";
	}
}
